<?php
/**************************
 * 
 *@name php分页类
 * 
 *@author  yanxiaoshan333@gmail.com
 * 
 *@version  1.01
 * 
 ****************************/
class Page{
	
	private $page;
	private $page_size;
	private $total_num;
	private $total_page;
	private $url;
	private $model;  //分页模式
	
	public  function __construct($total_num,$page_size=5,$model=1){
		$this->page=isset($_REQUEST['page'])&&$_REQUEST['page']>1?$_REQUEST['page']:1;		
		$this->page_size=$page_size;
		$this->total_num=$total_num;
		$this->total_page=ceil($total_num/$this->page_size);
		$this->model=$model;
		$this->url=$this->getUrl();
		//$this->limit=$this->getLimit();
		
	}
	//得到SQL中LIMIT
	public function getLimit(){
		return ($this->page-1)*$this->page_size.",".$this->page_size;
	}
	//得到URL
	private function getUrl(){
		
		$url_info=$this->url_info();
		$url=$this->bulid_url($url_info);
		
		return $url;
	}
	//拆分掉URL
	private function url_info(){
		$url=$_SERVER['REQUEST_URI'];
	    $url_info=parse_url($url);
		return $url_info;
	}
	//组合URL
	private function bulid_url($url_info){
		$base_url=$url_info['path'];
		$query_url=$url_info['query'];
		if(empty($query_url)){
			$url=$base_url."?";
		}else{
		    parse_str($query_url,$arr);
		    if(array_key_exists('page',$arr)){
		       unset($arr['page']);
		    }
		    $query=http_build_query($arr);
	
			$url=$base_url."?".$query."&";
		}
		return $url;
		
	}
	//首页
	private function first(){
	    if($this->page>1){
	        $first_url=$this->url."page=1";
	    	$str="<a href='$first_url'>首页</a>";
	    }
	    return $str;
	
	}
	//上一页
	private function prev(){
		if($this->page>1){
		    $prev_url=$this->url."page=".($this->page-1);
			$str="<a href='$prev_url'>上一页</a>";
		}
		return $str;
	}
	//下一页
	private function next(){
	    if($this->page<$this->total_page){
	    	$next_url=$this->url."page=".($this->page+1);
			$str="<a href='$next_url'>下一页</a>";
		}
		return $str;
	}
	
	//尾页
	private function last(){
		if($this->page<$this->total_page){
			
			$last_url=$this->url."page=".($this->total_page);
			$str="<a href='$last_url'>尾页</a>";
		}
		return $str;
	
	}
	//数字页
	private function num(){
		
		 $max=4; //最多显示4个
		 
		 $offset=$max/2;
		 
		 
		 if($this->page<=$max)
		 {
		 	  $max=$max<=$this->total_page?$max:$this->total_page;
		   	  for($i=1;$i<=$max;$i++)
		   	  {
		   		  if($i==$this->page) $str.="<span class='snum'>" .$i."</span>";
		   		  else $str.="<span class='num'><a href='".$this->url."page=".$i."'>" .$i."</a></span>";
		   	 }
		  
		 }
		 elseif($this->page+$max>=$this->total_page)
		 {
		 	  	for($i=$this->total_page-$max;$i<=$this->total_page;$i++)
		   	{
		   		if($i==$this->page) $str.="<span class='snum'>" .$i."</span>";
		   		else $str.="<span class='num'><a href='".$this->url."page=".$i."'>" .$i."</a></span>";
		   	}
		 }
		 else
		 {
	   for($i=$this->page-$offset;$i<$this->page;$i++)
	   {

	 			 $str.="<span class='num'><a href='".$this->url."page=".$i."'>" .$i."</a></span>";
	   	
	   }
	       $str.="<span class='snum'>" .$i."</span>";
	       
	   	for($i=$this->page+1;$i<=$this->page+$offset;$i++)
	   {

	 			 $str.="<span class='num'><a href='".$this->url."page=".$i."'>" .$i."</a></span>";
	   	
	   }
	 }
	 	return $str;
	}

	//显示分页
	public function show(){
		$str="共".$this->total_num."条记录 当前页为".$this->page."/".$this->total_page."&nbsp;";
		if($this->model==1) $str.=$this->first()."&nbsp;";
		$str.=$this->prev()."&nbsp;";
		if($this->model==2)
		{
			$str.=$this->num()."&nbsp;";
		}
		$str.=$this->next()."&nbsp;";
		if($this->model==1) $str.=$this->last()."&nbsp;";
		return $str;
	}
	
}
